package pikes.ecma;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class VariableStatement extends PrintableSourceElement implements Statement {

	private List<VariableDeclaration> variableDeclarations = new LinkedList<VariableDeclaration>();

	public VariableStatement(CharSequence identifier) {
		add(identifier);
	}

	public VariableStatement(CharSequence identifier, IAssignmentExpression assignmentExpression) {
		add(identifier, assignmentExpression);
	}

	public void add(CharSequence identifier) {
		variableDeclarations.add(new VariableDeclaration(identifier));
	}

	public void add(CharSequence identifier, IAssignmentExpression assignmentExpression) {
		variableDeclarations.add(new VariableDeclaration(identifier, assignmentExpression));
	}

	public void visit(SyntaxTreeVisitor syntaxTreeVisitor) throws EcmaSyntaxVisitingException {
		syntaxTreeVisitor.beforeVariableDeclarations();

		Iterator<VariableDeclaration> iterator = variableDeclarations.iterator();
		while (iterator.hasNext()) {
			iterator.next().visit(syntaxTreeVisitor);
			if (iterator.hasNext()) {
				syntaxTreeVisitor.comma();
			}
		}

		syntaxTreeVisitor.endOfStatement();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof VariableStatement) {
			VariableStatement thatVarStatement = (VariableStatement) obj;
			return variableDeclarations.equals(thatVarStatement.variableDeclarations);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return variableDeclarations.hashCode();
	}

}
